package com.isoft.demo01.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.demo01.dao.OrderDao;
import com.isoft.demo01.dao.impl.OrderRepository;
import com.isoft.demo01.entity.Order;

import com.isoft.demo01.service.OrderService;
import com.isoft.demo01.util.IdUtil;
import com.isoft.demo01.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service("orderServiceSys")
public class OrderImplService implements OrderService {
    @Autowired
    OrderDao orderDao;
    @Autowired
    OrderRepository orderRepository;
    public List<Order> find(Integer typeid){

        return orderDao.find(typeid);
    }
    @Override
    public boolean add(Order order) {
        if(null == order) {
            return false ;
        }
        if(IdUtil.idcheck(order.getUserid())) {
            return false ;
        }
        order.setOrdertime(new Date());
        return orderRepository.insert(order) > 0;
    }


    @Override
    public boolean delById(Integer id) {
        return orderRepository.deleteById(id)>0;
    }

    @Override
    public boolean delByIds(Integer[] ids) {
        return orderRepository.deleteByIds(Arrays.asList(ids))>0;
    }

    @Override
    public boolean update(Order order) {
        if(null == order){
            return false ;
        }
        if(!IdUtil.idcheck(order.getId())) {
            return false ;
        }
        Order n = new Order() ;
        n.setId(order.getId());
        n.setOrderid(order.getOrderid());
        n.setUserid(order.getUserid());
        n.setAllprice(order.getAllprice());
        n.setStatus(order.getStatus());
        return orderRepository.updateById(n) > 0;
    }

    @Override
    public Page<Order> getMore(Integer current, Integer size, Integer orderid, String ordertime, Integer allprice) {
        if(null == current || current < 1) {
            current = 1 ;
        }
        if(null == size || size < 1) {
            size = 10 ;
        }
        if(null == orderid || orderid < 1) {
            orderid = null ;
        }
        if(StringUtil.isEmpty(ordertime)){
            ordertime = null ;
        }
        if(null == allprice || allprice < 1) {
            allprice = null ;
        }
        Page<Order> page = orderRepository.page(current , size ,orderid , allprice,ordertime);
        return page ;
    }

    @Override
    public Order getById(Integer id) {
        if (!IdUtil.idcheck(id)) {
            return null;
        }
        return orderRepository.getDetailById(id);
    }

    @Override
    public boolean updatedel(Integer typeid, Boolean status) {
        if (!IdUtil.idcheck(typeid)) {
            return false;
        }
        int del=0;
        if (status!=null&&status){
            del=1;
        }
        return orderRepository.updateDel(typeid,del)>0;

    }

}
